package com.mb.app.controller;

import com.mb.app.domain.TbShipCfg;
import com.mb.app.service.ITbShipCfgService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @author zwl
 * @program
 * @description 物流快递配置管理
 * @date 2019-04-1 18:01
 **/
@Controller
@RequestMapping("/app/shipCfg")
@Slf4j
public class ShipCfgController extends BaseController {
    
    
    @Autowired
    private ITbShipCfgService shipCfgService;

    @GetMapping("/index")
    String index(){
        return "/app/shipCfg/shipCfg_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbShipCfg> list = shipCfgService.queryList(query);
        int total = shipCfgService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(){
        return "/app/shipCfg/shipCfg_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbShipCfg TbShipCfg, HttpServletRequest request){
        TbShipCfg.setAddUserId(getUserId());
        int result = shipCfgService.save(TbShipCfg);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("shipId") Long shipId, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = shipCfgService.remove(shipId,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{shipCfgId}")
    String dtl(@PathVariable("shipCfgId") Long shipCfg, Model model){
        model.addAttribute("shipCfgInfo",shipCfgService.getDtl(shipCfg));
        return "/app/shipCfg/shipCfg_info";
    }


    @GetMapping("/edit/{shipCfgId}")
    String edit(@PathVariable("shipCfgId") Long shipCfgId, Model model){
        model.addAttribute("shipCfgInfo",shipCfgService.getDtl(shipCfgId));
        return "/app/shipCfg/shipCfg_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbShipCfg TbShipCfg, HttpServletRequest request){
        TbShipCfg.setUpdateUserId(getUserId());
        int result = shipCfgService.update(TbShipCfg);
        return R.ok();
    }

    /**
     * 物流快递名称快递代码是否存在
     */
    @PostMapping("/shipCfgNameExit")
    @ResponseBody
    boolean shipCfgsNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !shipCfgService.shipCfgsNameExit(params);
    }



}
